package com.ilop.sthome.mvp.present.device;

import android.content.Context;
import android.os.Handler;

import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.common.command.SendOtherCommand;
import com.ilop.sthome.data.bean.DeviceHistoryBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.bean.HistoryBean;
import com.ilop.sthome.data.bean.WarnBean;
import com.ilop.sthome.mvp.contract.device.DeviceHistoryContract;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.example.common.utils.DateUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author skygge
 * @date 2020-01-14.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：历史记录
 */
public class DeviceHistoryPresenter extends BasePresenterImpl<DeviceHistoryContract.IView> implements DeviceHistoryContract.present {

    private Context mContext;
    private String mDeviceName;
    private Handler mHandler;
    private List<WarnBean> mWarnBeanList;
    private List<HistoryBean> mSubHistoryList;
    private List<DeviceHistoryBean> mHistoryList;
    private SendOtherCommand mSendDeviceHistory;

    private DeviceInfoBean mDeviceInfo;

    public DeviceHistoryPresenter(Context context) {
        this.mContext = context;
        mHandler = new Handler();
        mWarnBeanList = new ArrayList<>();
        mSubHistoryList = new ArrayList<>();
        mHistoryList = new ArrayList<>();
    }

    @Override
    public void getGatewayByName(String deviceName) {
        this.mDeviceName = deviceName;
        DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(deviceName);
        mSendDeviceHistory = new SendOtherCommand(mContext, gateway);
    }

    @Override
    public void getDeviceInfo(String deviceName, int deviceId) {
        this.mDeviceName = deviceName;
        DeviceInfoBean gateway = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
        mSendDeviceHistory = new SendOtherCommand(mContext, gateway);
        mDeviceInfo = DeviceDaoUtil.getInstance().findByDeviceId(deviceName, deviceId);
    }

    @Override
    public void sendGateWaySync(int num) {
        mSendDeviceHistory.syncAlarms(num);
    }

    @Override
    public void syncSubAlarm(int page, int deviceId) {
        mSendDeviceHistory.syncSubAlarms(page, deviceId);
    }

    @Override
    public void getHistoryList(List<WarnBean> list, int page) {
        if (page == 1){
            mWarnBeanList.clear();
        }
        mWarnBeanList.addAll(list);
        if (mWarnBeanList.size() > 0) {
            Map<String, List<WarnBean>> map = new HashMap<>();
            for (WarnBean warnBean : mWarnBeanList) {
                String time = DateUtil.getDateToString2(warnBean.getTime()*1000);
                if (map.containsKey(time)) {
                    Objects.requireNonNull(map.get(time)).add(warnBean);
                } else {
                    List<WarnBean> newList = new ArrayList<>();
                    newList.add(warnBean);
                    map.put(time, newList);
                }
            }
            mHistoryList.clear();
            for (Map.Entry<String, List<WarnBean>> entry : map.entrySet()) {
                String dayTime = entry.getKey();
                DeviceHistoryBean device = new DeviceHistoryBean();
                device.setYear(Integer.parseInt(dayTime.split("-")[0]));
                device.setMonth(Integer.parseInt(dayTime.split("-")[1]));
                device.setDay(Integer.parseInt(dayTime.split("-")[2]));
                device.setDaytime(dayTime);
                device.setNumber(entry.getValue().size());
                device.setList(entry.getValue());
                mHistoryList.add(device);
            }
            Collections.sort(mHistoryList);
            Collections.reverse(mHistoryList);
            mHandler.post(()-> mView.showHistory(mHistoryList));
        } else {
            mHandler.post(()->mView.withoutData());
        }
    }

    @Override
    public void getSubHistoryList(List<HistoryBean> list, int page) {
        if (page == 1){
            mSubHistoryList.clear();
        }
        mSubHistoryList.addAll(list);
        try {
            if (mSubHistoryList.size() > 0) {
                Map<String, List<HistoryBean>> map = new HashMap<>();
                for (HistoryBean history : mSubHistoryList) {
                    if (mDeviceInfo.getDevice_type().equals(history.getDevice_type())) {
                        String time = DateUtil.getDateToString2(history.getTime() * 1000);
                        if (map.containsKey(time)) {
                            Objects.requireNonNull(map.get(time)).add(history);
                        } else {
                            List<HistoryBean> newList = new ArrayList<>();
                            newList.add(history);
                            map.put(time, newList);
                        }
                    }
                }
                mHistoryList.clear();
                for (Map.Entry<String, List<HistoryBean>> entry : map.entrySet()) {
                    String dayTime = entry.getKey();
                    DeviceHistoryBean device = new DeviceHistoryBean();
                    device.setYear(Integer.parseInt(dayTime.split("-")[0]));
                    device.setMonth(Integer.parseInt(dayTime.split("-")[1]));
                    device.setDay(Integer.parseInt(dayTime.split("-")[2]));
                    device.setDaytime(dayTime);
                    device.setNumber(entry.getValue().size());
                    device.setSubList(entry.getValue());
                    mHistoryList.add(device);
                }
                Collections.sort(mHistoryList);
                Collections.reverse(mHistoryList);
                mHandler.post(() -> mView.showHistory(mHistoryList));
            } else {
                mHandler.post(() -> mView.withoutData());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteGatewayAlarm() {
        mSendDeviceHistory.deleteGatewayAlarms(255);
    }

    @Override
    public void deleteSubAlarm(int deviceId) {
        mSendDeviceHistory.deleteSubAlarms(255, deviceId);
    }
}
